//
//  ParserToTOTEM.java
//  TopoManipApplet
//
//  Created by Thibaud PIQUET on 08/04/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

package src.convertisseur;
import java.util.*;
import java.io.*;
import org.jdom.*;
import java.net.*;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.*;
import org.xml.sax.SAXException;
import java.net.*;

/**
 *
 * @author Thibaud PIQUET
 */
public class ParserToTOTEM implements Runnable {
	
    File ftp; // File to parse
	File fileOut; // File to Create
	
    public ParserToTOTEM(File file, File fileOUT) throws IOException {
        this.ftp = file;
		this.fileOut = fileOUT;
    }

	public void run() {
		try {
			this.parseToTOTEM();
		}
		catch (Exception e) {
			e.printStackTrace();
			Resume result = Resume.getInstance();
			result.add("ParserToTOTEM","procedure run had some problems : "+e);
		}
	}
		
    public void parseToTOTEM() throws IOException {
		
        Element domain;
		Element information;
		Element topology;
		Element igp;
		Element bgp;
		Element mpls;
		Element nodes;
		Element linksTopo;
		Element igpLinks;
        Element bgpRouters;
		
        Element domainTOTEM = new Element("domain");
		Element informationTOTEM = new Element("info");
		Element topologyTOTEM = new Element("topology");
		Element igpTOTEM = new Element("igp");
		Element bgpTOTEM = new Element("bgp");
		Element mplsTOTEM = new Element("mpls");
		Element nodesTOTEM = new Element("nodes");
		Element linksTOTEM = new Element("links");
		Element igpLinksTOTEM = new Element("links");
        Element bgpRoutersTOTEM = new Element("routers");
		
        org.jdom.Document documentTOTEM = new Document(domainTOTEM);
		
        boolean testOfUnit = false;
		
		Resume result = Resume.getInstance();
		
		try {
			
			// *********************************************************
			// Default values for TOTEM if there aren't in the XML file
			// ASID : 1
			// mask : 32
			// *********************************************************
            
			/************************************************************ Loading default values *************************************************************/
			InputStream inDefaultValues = getClass().getResourceAsStream("/resources/configuration.xml");
			
			org.jdom.input.SAXBuilder builderConfig = new org.jdom.input.SAXBuilder();
            builderConfig.setIgnoringElementContentWhitespace(true);
            org.jdom.Document documentDefaultValues = builderConfig.build(inDefaultValues);
			
			Element defaultValues = documentDefaultValues.getRootElement();
			
			String defaultValue_Domain_AS="", defaultValue_Node_mask="";
			
			List listOfFormats = defaultValues.getChildren();
			Iterator ilistOfFormats = listOfFormats.iterator();
			while(ilistOfFormats.hasNext()){
				Element currentChildOfDefaultValues = (Element)ilistOfFormats.next();
				
				if(currentChildOfDefaultValues.toString().equals("[Element: <format/>]")) {
					
					if(currentChildOfDefaultValues.getAttributeValue("name").equals("TOTEM")){
						
						List listOfDefaultValuesOfFormat = currentChildOfDefaultValues.getChildren();
						Iterator ilistOfDefaultValuesOfFormat = listOfDefaultValuesOfFormat.iterator();
						while(ilistOfDefaultValuesOfFormat.hasNext()){
							Element currentChildOfFormat = (Element)ilistOfDefaultValuesOfFormat.next();
							
							if(currentChildOfFormat.toString().equals("[Element: <domain/>]")){
								
								List listOfChildOfDomain = currentChildOfFormat.getChildren();
								Iterator ilistOfChildOfDomain = listOfChildOfDomain.iterator();
								while(ilistOfChildOfDomain.hasNext()){
									Element currentUnit = (Element)ilistOfChildOfDomain.next();
									
									if(currentUnit.toString().equals("[Element: <unit/>]")){
										
										if(currentUnit.getAttributeValue("type").equals("ASID")){
											defaultValue_Domain_AS=currentUnit.getAttributeValue("value");
										}
									}
								}
							}
							
							if(currentChildOfFormat.toString().equals("[Element: <node/>]")){
								
								List listOfChildOfNode = currentChildOfFormat.getChildren();
								Iterator ilistOfChildOfNode = listOfChildOfNode.iterator();
								while(ilistOfChildOfNode.hasNext()){
									Element currentUnit = (Element)ilistOfChildOfNode.next();
									
									if(currentUnit.toString().equals("[Element: <unit/>]")){
										
										if(currentUnit.getAttributeValue("type").equals("mask")){
											defaultValue_Node_mask=currentUnit.getAttributeValue("value");
										}
									}
								}
							}
						}
					}
				}
			}				
			/*************************************************************************************************************************************************/
				
				org.jdom.input.SAXBuilder builder = new org.jdom.input.SAXBuilder();
				builder.setIgnoringElementContentWhitespace(true);
				org.jdom.Document document = builder.build(ftp);
				
				domain = document.getRootElement();
				
				/* Test to know domain son's */
				boolean BoolInformation = false;
				boolean BoolIGP = false;
				boolean BoolBGP = false;
				boolean BoolMPLS = false;
				boolean BoolTopology = false;
				
				List domainChildren = domain.getChildren();
				Iterator idomainChildren = domainChildren.iterator();
				while(idomainChildren.hasNext()) {
					Element currentChild = (Element)idomainChildren.next();
					if(currentChild.toString().equals("[Element: <information/>]")) { BoolInformation=true;}
					if(currentChild.toString().equals("[Element: <igp/>]")) { BoolIGP=true; }
					if(currentChild.toString().equals("[Element: <bgp/>]")) { BoolBGP=true;}
					if(currentChild.toString().equals("[Element: <mpls/>]")) { BoolMPLS=true;}
					if(currentChild.toString().equals("[Element: <topology/>]")) { BoolTopology=true;}
				}
				
				if(BoolInformation==true) {
					information = domain.getChild("information");
					
					List infoChildren = information.getChildren();
					Iterator iInfoChildren = infoChildren.iterator();
					while(iInfoChildren.hasNext()){
						Element currentChild = (Element)iInfoChildren.next();
						/* case title */
						if(currentChild.toString().equals("[Element: <title/>]")) {
							Element title = new Element("title");
							title.setText(currentChild.getText());
							informationTOTEM.addContent(title);
						}
						/* case date */
						if(currentChild.toString().equals("[Element: <date/>]")) {
							Element date = new Element("date");
							date.setText(currentChild.getText());
							informationTOTEM.addContent(date);
						}
						/* case author */
						if(currentChild.toString().equals("[Element: <author/>]")) {
							Element author = new Element("author");
							author.setText(currentChild.getText());
							informationTOTEM.addContent(author);
						}
						/* case description */
						if(currentChild.toString().equals("[Element: <description/>]")) {
							Element description = new Element("description");
							description.setText(currentChild.getText());
							informationTOTEM.addContent(description);
							
							/* case units */
							List contentOfInformationTOTEM = informationTOTEM.getChildren();
							Iterator icontentOfInformationTOTEM = contentOfInformationTOTEM.iterator();
							while(icontentOfInformationTOTEM.hasNext()){
								Element currentElement = (Element)icontentOfInformationTOTEM.next();
								
								if(currentElement.toString().equals("[Element: <units/>]")) {
									testOfUnit = true;
								}
							}
							
							if(testOfUnit==false){
								Element units = new Element("units");
								
								Element unit = new Element("unit");
								Element unit2 = new Element("unit");
								
								Attribute type = new Attribute("type","bandwidth");
								Attribute value = new Attribute("value","mbps");
								Attribute type2 = new Attribute("type","delay");
								Attribute value2 = new Attribute("value","ms");
								
								unit.setAttribute(type);
								unit.setAttribute(value);
								unit2.setAttribute(type2);
								unit2.setAttribute(value2);
								
								units.addContent(unit);
								units.addContent(unit2);
								informationTOTEM.addContent(units);
							}
						}
						/* case diff-serv */
						if(currentChild.toString().equals("[Element: <diff-serv/>]")) {
							/* case units */
							List contentOfInformationTOTEM = informationTOTEM.getChildren();
							Iterator icontentOfInformationTOTEM = contentOfInformationTOTEM.iterator();
							while(icontentOfInformationTOTEM.hasNext()){
								Element currentElement = (Element)icontentOfInformationTOTEM.next();
								
								if(currentElement.toString().equals("[Element: <units/>]")) {
									testOfUnit = true;
								}
							}
							
							if(testOfUnit==false){
								Element units = new Element("units");
								
								Element unit = new Element("unit");
								Element unit2 = new Element("unit");
								
								Attribute type = new Attribute("type","bandwidth");
								Attribute value = new Attribute("value","mbps");
								Attribute type2 = new Attribute("type","delay");
								Attribute value2 = new Attribute("value","ms");
								
								unit.setAttribute(type);
								unit.setAttribute(value);
								unit2.setAttribute(type2);
								unit2.setAttribute(value2);               
								
								units.addContent(unit);
								units.addContent(unit2);
								informationTOTEM.addContent(units);
							}
							
							
							Element diffServ = new Element("diff-serv");
							
							List listOfPriority = currentChild.getChildren();
							Iterator ilistOfPriority = listOfPriority.iterator();
							while(ilistOfPriority.hasNext()){
								Element currentPriority = (Element)ilistOfPriority.next();
								Element priority = new Element("priority");
								Attribute ct = new Attribute("ct",currentPriority.getAttributeValue("ct"));
								Attribute id = new Attribute("id",currentPriority.getAttributeValue("id"));
								Attribute preemption = new Attribute("preemption",currentPriority.getAttributeValue("preemption"));
								priority.setAttribute(ct);
								priority.setAttribute(id);
								priority.setAttribute(preemption);
								diffServ.addContent(priority);
							}
							informationTOTEM.addContent(diffServ);
						}
						/* case classes-of-service */
						if(currentChild.toString().equals("[Element: <classes-of-service/>]")) {
							Element classesServ = new Element("classes-of-service");
							
							List listOfCos = currentChild.getChildren();
							Iterator ilistOfCos = listOfCos.iterator();
							while(ilistOfCos.hasNext()){
								Element currentCos = (Element)ilistOfCos.next();
								Element cos = new Element("cos");
								
								List listOfSubClass = currentCos.getChildren();
								Iterator ilistOfSubClass = listOfSubClass.iterator();
								while(ilistOfSubClass.hasNext()){
									Element currentSubClass = (Element)ilistOfSubClass.next();
									Element subClass = new Element("sub-class");
									subClass.setText(currentSubClass.getText());
									cos.addContent(subClass);
								}
								
								Attribute name = new Attribute("name",currentCos.getAttributeValue("name"));
								cos.setAttribute(name);
								classesServ.addContent(cos);
							}
							informationTOTEM.addContent(classesServ);
						}
						/* case srlgs */
						if(currentChild.toString().equals("[Element: <srlgs/>]")) {
							Element srlgs = new Element("srlgs");
							
							List listOfSrlg = currentChild.getChildren();
							Iterator ilistOfSrlg = listOfSrlg.iterator();
							while(ilistOfSrlg.hasNext()){
								Element currentSrlg = (Element)ilistOfSrlg.next();
								Element srlg = new Element("srlg");
								Element id = new Element("id");
								id.setText(currentSrlg.getChildText("id"));
								srlg.addContent(id);
								srlgs.addContent(srlg);
							}
							informationTOTEM.addContent(srlgs);
						}
					}
				}
				
				/* Topology */
				if(BoolTopology==true) {
					topology = domain.getChild("topology");
					nodes = topology.getChild("nodes");
					linksTopo = topology.getChild("links");
					
					/* nodes */
					List listOfnode = nodes.getChildren();
					Iterator ilistOfnode = listOfnode.iterator();
					while(ilistOfnode.hasNext()){
						Element currentNode = (Element)ilistOfnode.next();
						Element node = new Element("node");
						Element description = new Element("description");
						Element status = new Element("status");
						Element rid = new Element("rid");
						Element type = new Element("type");
						Element location = new Element("location");
						Element interfaces = new Element("interfaces");
						
						List listOfChild = currentNode.getChildren();
						Iterator ilistOfChild = listOfChild.iterator();
						while(ilistOfChild.hasNext()) {
							Element currentChild = (Element)ilistOfChild.next();
							
							if(currentChild.toString().equals("[Element: <description/>]")) {
								description = new Element("description");
								description.setText(currentChild.getText());
							}
							
							if(currentChild.toString().equals("[Element: <status/>]")) {
								status = new Element("status");
								status.setText(currentChild.getText());
							}
							
							if(currentChild.toString().equals("[Element: <rid/>]")) {
								rid = new Element("rid");
								rid.setText(currentChild.getText());
							}
							
							if(currentChild.toString().equals("[Element: <nodeType/>]")) {
								List listOftypes = currentChild.getAttributes();
								Iterator ilistOftypes = listOftypes.iterator();
								while(ilistOftypes.hasNext()){
									Attribute currentAttributeType = (Attribute)ilistOftypes.next();
									if(currentAttributeType.getName().equals("type2")){
										type = new Element("type");
										type.setText(currentAttributeType.getValue());
									}
								}
							}
							
							if(currentChild.toString().equals("[Element: <location/>]")) {
								location = new Element("location");
								
								List listOfLocationAttribute = currentChild.getAttributes();
								Iterator ilistOfLocationAttribute = listOfLocationAttribute.iterator();
								while(ilistOfLocationAttribute.hasNext()){
									Attribute currentAttributeLocation = (Attribute)ilistOfLocationAttribute.next();
									
									if(currentAttributeLocation.getName().equals("latitude")){
										Attribute latitude = new Attribute("latitude",currentChild.getAttributeValue("latitude"));
										location.setAttribute(latitude);
									}
									
									if(currentAttributeLocation.getName().equals("longitude")){
										Attribute longitude = new Attribute("longitude",currentChild.getAttributeValue("longitude"));
										location.setAttribute(longitude);
									}
								}
							}
							
							if(currentChild.toString().equals("[Element: <interfaces/>]")) {
								interfaces = new Element("interfaces");
								
								List listOfInterface = currentChild.getChildren();
								Iterator ilistOfInterface = listOfInterface.iterator();
								while(ilistOfInterface.hasNext()) {
									Element currentInterface = (Element)ilistOfInterface.next();
									Element interfaceNode = new Element("interface");
									
									List listOfChildOfInterface = currentInterface.getChildren();
									Iterator ilistOfChildOfInterface = listOfChildOfInterface.iterator();
									while(ilistOfChildOfInterface.hasNext()) {
										Element currentChildOfInterface = (Element)ilistOfChildOfInterface.next();
										
										if(currentChildOfInterface.toString().equals("[Element: <status/>]")) {
											Element statusInterface = new Element("status");
											statusInterface.setText(currentChildOfInterface.getText());
											interfaceNode.addContent(statusInterface);
										}
										
										if(currentChildOfInterface.toString().equals("[Element: <ip/>]")) {
											Element ip = new Element("ip");
											
											List listOfAttributes = currentChildOfInterface.getAttributes();
											Iterator ilistOfAttributes = listOfAttributes.iterator();
											while(ilistOfAttributes.hasNext()){
												Attribute currentAttributeIP = (Attribute)ilistOfAttributes.next();
												
												if(currentAttributeIP.getName().equals("mask")){
													Attribute mask = new Attribute("mask",currentChildOfInterface.getAttributeValue("mask"));
													ip.setAttribute(mask);
												}
												
											}
											ip.setText(currentChildOfInterface.getText());
											if(ip.getAttributes().isEmpty()==true){
                                                Attribute mask = new Attribute("mask",currentChildOfInterface.getText()+defaultValue_Node_mask);
                                                ip.setAttribute(mask);
											}
											interfaceNode.addContent(ip);
										}
									}
									Attribute idInterface = new Attribute("id",currentInterface.getAttributeValue("id"));
									interfaceNode.setAttribute(idInterface);
									interfaces.addContent(interfaceNode);
								}
							}
						}
						Attribute idNode = new Attribute("id",currentNode.getAttributeValue("id"));
						
						node.setAttribute(idNode);
						if(!status.getText().equals("")) {node.addContent(status);}
						if(!rid.getText().equals("")) {node.addContent(rid);}
						if(!description.getText().equals("")) {node.addContent(description);}
						if(!type.getText().equals("")) {node.addContent(type);}
						if(location.getAttributes().isEmpty()==false) {node.addContent(location);}
						if(interfaces.getChildren().isEmpty()==false) {node.addContent(interfaces);}
						
						nodesTOTEM.addContent(node);
					}
					
					/* links */
					List listOfLink = linksTopo.getChildren();
					Iterator ilistOfLink = listOfLink.iterator();
					while(ilistOfLink.hasNext()){
						Element currentLink = (Element)ilistOfLink.next();
						Element link = new Element("link");
						Element from = new Element("from");
						Element to = new Element("to");
						Element description = new Element("description");
						Element status = new Element("status");
						Element bw = new Element("bw");
						Element delay = new Element("delay");
						Element type = new Element("type");
						Element technology = new Element("technology");
						Element srlgs = new Element("srlgs");
						
						List listOfChildOfLink = currentLink.getChildren();
						Iterator ilistOfChildOfLink = listOfChildOfLink.iterator();
						while(ilistOfChildOfLink.hasNext()){
							Element currentChild = (Element)ilistOfChildOfLink.next();
							
							//System.out.println("Element de link : "+currentChild.toString());
							
							if(currentChild.toString().equals("[Element: <source/>]")) {
								from = new Element("from");
								Attribute node = new Attribute("node",currentChild.getAttributeValue("node"));
								from.setAttribute(node);
								List listOfAttributesOfSource = currentChild.getAttributes();
								Iterator ilistOfAttributesOfSource = listOfAttributesOfSource.iterator();
								while(ilistOfAttributesOfSource.hasNext()){
									Attribute currentAttributeSource = (Attribute)ilistOfAttributesOfSource.next();
									
									if(currentAttributeSource.getName().equals("interface")){
										Attribute interf = new Attribute("if",currentChild.getAttributeValue("interface"));
										from.setAttribute(interf);
									}
								}
							}
							
							if(currentChild.toString().equals("[Element: <target/>]")) {
								to = new Element("to");
								Attribute node = new Attribute("node",currentChild.getAttributeValue("node"));
								to.setAttribute(node);
								List listOfAttributesOfSource = currentChild.getAttributes();
								Iterator ilistOfAttributesOfSource = listOfAttributesOfSource.iterator();
								while(ilistOfAttributesOfSource.hasNext()){
									Attribute currentAttributeSource = (Attribute)ilistOfAttributesOfSource.next();
									
									if(currentAttributeSource.getName().equals("interface")){
										Attribute interf = new Attribute("if",currentChild.getAttributeValue("interface"));
										from.setAttribute(interf);
									}
								}
							}
							
							if(currentChild.toString().equals("[Element: <status/>]")) {
								status = new Element("status");
								status.setText(currentChild.getText());
							}
							
							if(currentChild.toString().equals("[Element: <description/>]")) {
								description = new Element("description");
								description.setText(currentChild.getText());
							}
							
							if(currentChild.toString().equals("[Element: <linkType/>]")) {
								List listOftypes = currentChild.getAttributes();
								Iterator ilistOftypes = listOftypes.iterator();
								while(ilistOftypes.hasNext()){
									Attribute currentAttributeType = (Attribute)ilistOftypes.next();
									if(currentAttributeType.getName().equals("type4")){
										type = new Element("type");
										type.setText(currentAttributeType.getValue());
									}                                
								}
							}
							
							if(currentChild.toString().equals("[Element: <bandwidth/>]")) {
								bw = new Element("bw");
								bw.setText(currentChild.getText());
							}
							
							if(currentChild.toString().equals("[Element: <technology/>]")) {
								technology = new Element("technology");
								technology.setText(currentChild.getText());
							}
							
							if(currentChild.toString().equals("[Element: <delay/>]")) {
								delay = new Element("delay");
								delay.setText(currentChild.getText());
							}
							
							if(currentChild.toString().equals("[Element: <srlgs/>]")) {
								srlgs = new Element("srlgs");
								
								List listOfSrlg = currentChild.getChildren();
								Iterator ilistOfSrlg = listOfSrlg.iterator();
								while(ilistOfSrlg.hasNext()){
									Element currentSrlg = (Element)ilistOfSrlg.next();
									Element srlg = new Element("srlg");
									srlg.setText(currentSrlg.getText());
									srlgs.addContent(srlg);
								}
							}
							
						}
						Attribute idLink = new Attribute("id",currentLink.getAttributeValue("id"));
						
						link.setAttribute(idLink);
						if(from.getAttributes().isEmpty()==false) {link.addContent(from);}
						if(to.getAttributes().isEmpty()==false) {link.addContent(to);}
						if(!description.getText().equals("")) {link.addContent(description);}
						if(!status.getText().equals("")) {link.addContent(status);}
						if(!bw.getText().equals("")) {link.addContent(bw);}
						if(!delay.getText().equals("")) {link.addContent(delay);}
						if(!type.getText().equals("")) {link.addContent(type);}
						if(!technology.getText().equals("")) {link.addContent(technology);}
						if(srlgs.getChildren().isEmpty()==false) {link.addContent(srlgs);}
						
						linksTOTEM.addContent(link);
					}
				}
				
				
				/* MPLS */
				if(BoolMPLS==true){
					mpls = domain.getChild("mpls");
					
					List listOfLspFromMpls = mpls.getChildren();
					Iterator ilistOfLspFromMpls = listOfLspFromMpls.iterator();
					while(ilistOfLspFromMpls.hasNext()) {
						Element currentLsp = (Element)ilistOfLspFromMpls.next();
						
						Element lsp = new Element("lsp");
						Element path = new Element("path");
						Element bw = new Element("bw");
						Element metric = new Element("metric");
						Element maxRate = new Element("max_rate");
						Element diffServ = new Element("diff-serv");
						Element acceptedCos = new Element("accepted_cos");
						Element backup = new Element("backup");
						
						List listOfChildOfLsp = currentLsp.getChildren();
						Iterator ilistOfChildOfLsp = listOfChildOfLsp.iterator();
						while(ilistOfChildOfLsp.hasNext()) {
							Element currentChildOfLsp = (Element)ilistOfChildOfLsp.next();
							
							if(currentChildOfLsp.toString().equals("[Element: <path/>]")) {
								path = new Element("path");
								
								List listOfChildOfPath = currentChildOfLsp.getChildren();
								Iterator ilistOfChildOfPath = listOfChildOfPath.iterator();
								while(ilistOfChildOfPath.hasNext()) {
									Element currentLinkOfPath = (Element)ilistOfChildOfPath.next();
									
									Element link = new Element("link");
									link.setText(currentLinkOfPath.getText());
									path.addContent(link);
								}
								lsp.addContent(path);
							}
							
							if(currentChildOfLsp.toString().equals("[Element: <bw/>]")) {
								bw = new Element("bw");
								bw.setText(currentChildOfLsp.getText());
								lsp.addContent(bw);
							}
							
							if(currentChildOfLsp.toString().equals("[Element: <metric/>]")) {
								metric = new Element("metric");
								metric.setText(currentChildOfLsp.getText());
								lsp.addContent(metric);
							}
							
							if(currentChildOfLsp.toString().equals("[Element: <max_rate/>]")) {
								maxRate = new Element("max_rate");
								maxRate.setText(currentChildOfLsp.getText());
								lsp.addContent(maxRate);
							}
							
							if(currentChildOfLsp.toString().equals("[Element: <diff-serv/>]")) {
								diffServ = new Element("diff-serv");
								
								List listOfChildOfDiffServ = currentChildOfLsp.getChildren();
								Iterator ilistOfChildOfDiffServ = listOfChildOfDiffServ.iterator();
								
								while(ilistOfChildOfDiffServ.hasNext()){
									Element currentChildOfDiffServ = (Element)ilistOfChildOfDiffServ.next();
									
									if(currentChildOfDiffServ.toString().equals("[Element: <ct/>]")) {
										Element ct = new Element("ct");
										ct.setText(currentChildOfDiffServ.getText());
										diffServ.addContent(ct);
									}
									
									if(currentChildOfDiffServ.toString().equals("[Element: <preemption/>]")) {
										Element preemption = new Element("preemption");
										Attribute setup = new Attribute("setup",currentChildOfDiffServ.getAttributeValue("setup"));
										Attribute holding = new Attribute("holding",currentChildOfDiffServ.getAttributeValue("holding"));
										preemption.setAttribute(setup);
										preemption.setAttribute(holding);
										diffServ.addContent(preemption);
									}
								}
								lsp.addContent(diffServ);
							}
							
							if(currentChildOfLsp.toString().equals("[Element: <accepted_cos/>]")) {
								acceptedCos = new Element("accepted_cos");
								
								List listOfChildOfAcceptedCos = currentChildOfLsp.getChildren();
								Iterator ilistOfChildOfAcceptedCos = listOfChildOfAcceptedCos.iterator();
								
								while(ilistOfChildOfAcceptedCos.hasNext()){
									Element currentChildOfAcceptedCos = (Element)ilistOfChildOfAcceptedCos.next();
									
									if(currentChildOfAcceptedCos.toString().equals("[Element: <cos/>]")){
										Element cos = new Element("cos");
										cos.setText(currentChildOfAcceptedCos.getText());
										acceptedCos.addContent(cos);
									}
								}
								
								lsp.addContent(acceptedCos);
							}
							
							if(currentChildOfLsp.toString().equals("[Element: <backup/>]")) {
								backup = new Element("backup");
								
								List listOfChildOfBackup = currentChildOfLsp.getChildren();
								Iterator ilistOfChildOfBackup = listOfChildOfBackup.iterator();
								while(ilistOfChildOfBackup.hasNext()){
									Element currentChildOfBackup = (Element)ilistOfChildOfBackup.next();
									
									if(currentChildOfBackup.toString().equals("[Element: <protected_lsp/>]")) {
										Element protectedLsp = new Element("protected_lsp");
										protectedLsp.setText(currentChildOfBackup.getText());
										backup.addContent(protectedLsp);
									}
									
									if(currentChildOfBackup.toString().equals("[Element: <protected_links/>]")) {
										Element protectedLinks = new Element("protected_links");
										
										List listOfChildOfProtectedLinks = currentChildOfBackup.getChildren();
										Iterator ilistOfChildOfProtectedLinks = listOfChildOfProtectedLinks.iterator();
										while(ilistOfChildOfProtectedLinks.hasNext()){
											Element currentChildOfProtectedLinks = (Element)ilistOfChildOfProtectedLinks.next();
											
											if(currentChildOfProtectedLinks.toString().equals("[Element: <protected_link/>]")) {
												Element protectedLink = new Element("protected_link");
												protectedLink.setText(currentChildOfProtectedLinks.getText());
												protectedLinks.addContent(protectedLink);
											}
										}
										backup.addContent(protectedLinks);
									}
								}
								
								Attribute type = new Attribute("type",currentChildOfLsp.getAttributeValue("type"));
								backup.setAttribute(type);
								lsp.addContent(backup);
							}
						}
						mplsTOTEM.addContent(lsp);
					}
				}
				
				
				/* IGP */
				if(BoolIGP==true){
					igp = domain.getChild("igp");
					igpLinks = igp.getChild("links");
					
					List listOfLinkIgp = igpLinks.getChildren();
					Iterator ilistOfLinkIgp = listOfLinkIgp.iterator();
					while(ilistOfLinkIgp.hasNext()){
						Element currentLinkIgp = (Element)ilistOfLinkIgp.next();
						Element igpLink = new Element("link");
						Element staticLink = new Element("static");
						Element dynamicLink = new Element("dynamic");
						Element metric = new Element("metric");
						Element teMetric = new Element("te-metric");
						Element mrbw = new Element("mrbw");
						Element mbw = new Element("mbw");
						Element admingroup = new Element("admingroup");
						Element diffServIgp = new Element("diff-serv");
						Element rbw = new Element("rbw");
						
						List listOfChildOfLinkIgp = currentLinkIgp.getChildren();
						Iterator ilistOfChildOfLinkIgp = listOfChildOfLinkIgp.iterator();
						while(ilistOfChildOfLinkIgp.hasNext()){
							Element currentChildOfLinkIgp = (Element)ilistOfChildOfLinkIgp.next();
							
							if(currentChildOfLinkIgp.toString().equals("[Element: <static/>]")){
								staticLink = new Element("static");
								
								List listOfChildOfStaticInIgpLink = currentChildOfLinkIgp.getChildren();
								Iterator ilistOfChildOfStaticInIgpLink = listOfChildOfStaticInIgpLink.iterator();
								while(ilistOfChildOfStaticInIgpLink.hasNext()){
									Element currentChildOfStatic = (Element)ilistOfChildOfStaticInIgpLink.next();
									
									if(currentChildOfStatic.toString().equals("[Element: <metric/>]")) {
										metric = new Element("metric");
										metric.setText(currentChildOfStatic.getText());
									}
									
									if(currentChildOfStatic.toString().equals("[Element: <te-metric/>]")) {
										teMetric = new Element("te-metric");
										teMetric.setText(currentChildOfStatic.getText());
									}
									
									if(currentChildOfStatic.toString().equals("[Element: <mrbw/>]")) {
										mrbw = new Element("mrbw");
										mrbw.setText(currentChildOfStatic.getText());
									}
									
									if(currentChildOfStatic.toString().equals("[Element: <mbw/>]")) {
										mbw = new Element("mbw");
										mbw.setText(currentChildOfStatic.getText());
									}
									
									if(currentChildOfStatic.toString().equals("[Element: <admingroup/>]")) {
										admingroup = new Element("admingroup");
										admingroup.setText(currentChildOfStatic.getText());
									}
									
									if(currentChildOfStatic.toString().equals("[Element: <diff-serv/>]")) {
										diffServIgp = new Element("diff-serv");
										
										List listOfChildOfDiffServOfIgpLink = currentChildOfStatic.getChildren();
										Iterator ilistOfChildOfDiffServOfIgpLink = listOfChildOfDiffServOfIgpLink.iterator();
										while(ilistOfChildOfDiffServOfIgpLink.hasNext()){
											Element currentChildOfDiffServ = (Element)ilistOfChildOfDiffServOfIgpLink.next();
											
											if(currentChildOfDiffServ.toString().equals("[Element: <bcm/>]")) {
												Element bcm = new Element("bcm");
												bcm.setText(currentChildOfDiffServ.getText());
												diffServIgp.addContent(bcm);
											}
											
											if(currentChildOfDiffServ.toString().equals("[Element: <bc/>]")) {
												Element bc = new Element("bc");
												bc.setText(currentChildOfDiffServ.getText());
												Attribute id = new Attribute("id",currentChildOfDiffServ.getAttributeValue("id"));
												bc.setAttribute(id);
												diffServIgp.addContent(bc);
											}
										}
										
										if(!metric.getText().equals("")) { staticLink.addContent(metric); }
										if(!teMetric.getText().equals("")) { staticLink.addContent(teMetric); }
										if(!mrbw.getText().equals("")) { staticLink.addContent(mrbw); }
										if(!mbw.getText().equals("")) { staticLink.addContent(mbw); }
										if(!admingroup.getText().equals("")) { staticLink.addContent(admingroup); }
										if(diffServIgp.getChildren().isEmpty()==false) { staticLink.addContent(diffServIgp); }
									}
								}
							}
							
							if(currentChildOfLinkIgp.toString().equals("[Element: <dynamic/>]")){
								dynamicLink = new Element("dynamic");
								
								List listOfChildOfDynamicLink = currentChildOfLinkIgp.getChildren();
								Iterator ilistOfChildOfDynamicLink = listOfChildOfDynamicLink.iterator();
								while(ilistOfChildOfDynamicLink.hasNext()){
									Element currentChildOfDynamic = (Element)ilistOfChildOfDynamicLink.next();
									
									if(currentChildOfDynamic.toString().equals("[Element: <rbw/>]")) {
										rbw = new Element("rbw");
										
										List listOfPriority = currentChildOfDynamic.getChildren();
										Iterator ilistOfPriority = listOfPriority.iterator();
										while(ilistOfPriority.hasNext()) {
											Element currentPriority = (Element)ilistOfPriority.next();
											
											Element priority = new Element("priority");
											priority.setText(currentPriority.getText());
											Attribute id = new Attribute("id",currentPriority.getAttributeValue("id"));
											priority.setAttribute(id);
											rbw.addContent(priority);
										}
									}
									
									if(rbw.getChildren().isEmpty()==false) { dynamicLink.addContent(rbw); }
								}
								
							}
						}
						Attribute idIgpLink = new Attribute("id",currentLinkIgp.getAttributeValue("id"));
						igpLink.setAttribute(idIgpLink);
						
						if(staticLink.getChildren().isEmpty()==false) { igpLink.addContent(staticLink); }
						if(dynamicLink.getChildren().isEmpty()==false) { igpLink.addContent(dynamicLink); }
						
						igpLinksTOTEM.addContent(igpLink);
					}
				}
				
				/* BGP */
				if(BoolBGP==true){
					bgp = domain.getChild("bgp");
					bgpRouters = bgp.getChild("routers");
					
					List listOfRouter = bgpRouters.getChildren();
					Iterator ilistOfRouter = listOfRouter.iterator();
					while(ilistOfRouter.hasNext()){
						Element currentRouter = (Element)ilistOfRouter.next();
						Element router = new Element("router");
						Element rid = new Element("rid");
						Element networks = new Element("networks");
						Element neighbors = new Element("neighbors");
						
						List listOfChildOfRouter = currentRouter.getChildren();
						Iterator ilistOfChildOfRouter = listOfChildOfRouter.iterator();
						while(ilistOfChildOfRouter.hasNext()){
							Element currentChildOfRouter = (Element)ilistOfChildOfRouter.next();
							
							if(currentChildOfRouter.toString().equals("[Element: <rid/>]")) {
								rid = new Element("rid");
								rid.setText(currentChildOfRouter.getText());
								router.addContent(rid);
							}
							
							if(currentChildOfRouter.toString().equals("[Element: <networks/>]")) {
								networks = new Element("networks");
								
								List listOfNetwork = currentChildOfRouter.getChildren();
								Iterator ilistOfNetwork = listOfNetwork.iterator();
								while(ilistOfNetwork.hasNext()) {
									Element currentNetwork = (Element)ilistOfNetwork.next();
									
									if(currentNetwork.toString().equals("[Element: <network/>]")){
										Element network = new Element("network");
										Attribute prefix = new Attribute("prefix",currentNetwork.getAttributeValue("prefix"));
										network.setAttribute(prefix);
										networks.addContent(network);
									}
								}
								router.addContent(networks);
							}
							
							if(currentChildOfRouter.toString().equals("[Element: <neighbors/>]")) {
								neighbors = new Element("neighbors");
								
								List listOfNeighbors = currentChildOfRouter.getChildren();
								Iterator ilistOfNeighbors = listOfNeighbors.iterator();
								while(ilistOfNeighbors.hasNext()){
									Element currentNeighbor = (Element)ilistOfNeighbors.next();
									
									Element neighbor = new Element("neighbor");
									
									List listOfFilters = currentNeighbor.getChildren();
									Iterator ilistOfFilters = listOfFilters.iterator();
									while(ilistOfFilters.hasNext()){
										Element currentFilters = (Element)ilistOfFilters.next();
										
										if(currentFilters.toString().equals("[Element: <filters/>]")) {
											Element filters = new Element("filters");
											
											List listOfFilter = currentFilters.getChildren();
											Iterator ilistOfFilter = listOfFilter.iterator();
											while(ilistOfFilter.hasNext()){
												Element currentFilter = (Element)ilistOfFilter.next();
												
												if(currentFilter.toString().equals("[Element: <in-filter/>]")) {
													Element inFilter = new Element("in-filter");
													Element rule = new Element("rule");
													
													List listOfActionsOfRule = currentFilter.getChild("rule").getChildren();
													Iterator ilistOfActionsOfRule = listOfActionsOfRule.iterator();
													
													while(ilistOfActionsOfRule.hasNext()){
														Element currentAction = (Element)ilistOfActionsOfRule.next();
														
														if(currentAction.toString().equals("[Element: <action/>]")) {
															Element action = new Element("action");
															Attribute type = new Attribute("type",currentAction.getAttributeValue("type"));
															Attribute value = new Attribute("value",currentAction.getAttributeValue("value"));
															action.setAttribute(type);
															action.setAttribute(value);
															rule.addContent(action);
														}
													}
													inFilter.addContent(rule);
													filters.addContent(inFilter);
												}
												
												if(currentFilter.toString().equals("[Element: <out-filter/>]")) {
													Element outFilter = new Element("out-filter");
													Element rule = new Element("rule");
													
													List listOfActionsOfRule = currentFilter.getChild("rule").getChildren();
													Iterator ilistOfActionsOfRule = listOfActionsOfRule.iterator();
													
													while(ilistOfActionsOfRule.hasNext()){
														Element currentAction = (Element)ilistOfActionsOfRule.next();
														
														if(currentAction.toString().equals("[Element: <action/>]")) {
															Element action = new Element("action");
															Attribute type = new Attribute("type",currentAction.getAttributeValue("type"));
															Attribute value = new Attribute("value",currentAction.getAttributeValue("value"));
															action.setAttribute(type);
															action.setAttribute(value);
															rule.addContent(action);
														}
													}
													outFilter.addContent(rule);
													filters.addContent(outFilter);
												}
											}
										}
									}
									
									Attribute AS = new Attribute("as",currentNeighbor.getAttributeValue("as"));
									neighbor.setAttribute(AS);
									Attribute ip = new Attribute("ip",currentNeighbor.getAttributeValue("ip"));
									neighbor.setAttribute(ip);
									
									List listOfAttributeOfNeighbor = currentNeighbor.getAttributes();
									Iterator ilistOfAttributeOfNeighbor = listOfAttributeOfNeighbor.iterator();
									while(ilistOfAttributeOfNeighbor.hasNext()){
										Attribute currentAttribute = (Attribute)ilistOfAttributeOfNeighbor.next();
										
										if(currentAttribute.getName().equals("reflector-client")) {
											Attribute reflectorClient = new Attribute("reflector-client",currentNeighbor.getAttributeValue("reflector-client"));
											neighbor.setAttribute(reflectorClient);
										}
										
										if(currentAttribute.getName().equals("next-hop-self")) {
											Attribute nextHopSelf = new Attribute("next-hop-self",currentNeighbor.getAttributeValue("next-hop-self"));
											neighbor.setAttribute(nextHopSelf);
										}
									}
									neighbors.addContent(neighbor);
								}
								router.addContent(neighbors);
							}
						}
						List listOfAttributesOfRouter = currentRouter.getAttributes();
						Iterator ilistOfAttributesOfRouter = listOfAttributesOfRouter.iterator();
						while(ilistOfAttributesOfRouter.hasNext()){
							Attribute currentAttributeOfRouter = (Attribute)ilistOfAttributesOfRouter.next();
							
							if(currentAttributeOfRouter.getName().equals("reflector")) {
								Attribute reflector = new Attribute("reflector",currentRouter.getAttributeValue("reflector"));
								router.setAttribute(reflector);
							}
							
							if(currentAttributeOfRouter.getName().equals("id")) {
								Attribute id = new Attribute("id",currentRouter.getAttributeValue("id"));
								router.setAttribute(id);
							}
						}
						bgpRoutersTOTEM.addContent(router);
					}
				}
				
				if (BoolInformation==true){
					domainTOTEM.addContent(informationTOTEM);
				}
				
				if(BoolTopology==true){
					topologyTOTEM.addContent(nodesTOTEM);
					topologyTOTEM.addContent(linksTOTEM);
					domainTOTEM.addContent(topologyTOTEM);
				}
				
				if(BoolMPLS==true){
					domainTOTEM.addContent(mplsTOTEM);
				}
				
				if(BoolIGP==true){
					igpTOTEM.addContent(igpLinksTOTEM);
					domainTOTEM.addContent(igpTOTEM);
				}
				
				if(BoolBGP==true){
					bgpTOTEM.addContent(bgpRoutersTOTEM);
					domainTOTEM.addContent(bgpTOTEM);
				}
				
				List attributesDomain = domain.getAttributes();
				Iterator iattributesDomain = attributesDomain.iterator();
				while(iattributesDomain.hasNext()) {
					Attribute currentAttribute = (Attribute)iattributesDomain.next();
					//System.out.println("Attribut domain : "+currentAttribute.getName());
					if(currentAttribute.getName().equals("ASID")) {
						Attribute ASID = new Attribute("ASID",currentAttribute.getValue());
						domainTOTEM.setAttribute(ASID);
					}
					if(currentAttribute.getName().equals("name")) {
						Attribute name = new Attribute("name",currentAttribute.getValue());
						domainTOTEM.setAttribute(name);
					}
					if(currentAttribute.getName().equals("xmlns:xsi")) {
						Attribute xmlns = new Attribute("xmlns:xsi",currentAttribute.getValue());
						domainTOTEM.setAttribute(xmlns);
					}
					if(currentAttribute.getName().equals("xsi:noNamespaceSchemaLocation")) {
						Attribute xsi = new Attribute("xsi:noNamespaceSchemaLocation",currentAttribute.getValue());
						domainTOTEM.setAttribute(xsi);
					}
				}
				
				if(domain.getAttributes().isEmpty()==true){
					Attribute ASID = new Attribute("ASID",defaultValue_Domain_AS);
                    domainTOTEM.setAttribute(ASID);               
				}
			
            org.jdom.output.XMLOutputter sortie = new org.jdom.output.XMLOutputter(org.jdom.output.Format.getPrettyFormat());
            sortie.output(documentTOTEM, new FileOutputStream(fileOut.getAbsolutePath()));
			
            //ValidatorXML test = new ValidatorXML(fileOUT, new URL("http://totem.run.montefiore.ulg.ac.be/Schema/Domain-v1_3.xsd"));
            //test.validationURL();
			result.add("ParserToTOTEM","Export succeeded");
			
		} catch (Exception e) {
			e.printStackTrace();
			result.add("ParserToTOTEM","Export had problems "+e);
		}
		
		
    }
}